package com.yuda.tools.log.utils;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期时间类
 *
 * @author baiansen
 *
 */
public class DateUtils {
    public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final String DATE_TIME_FORMAT2 = "yyyy-MM-dd_HH-mm-ss";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_TIME_FORMAT_2 = "yyyy-MM-dd HH:mm";
    private static final String timePattern = "HH:mm:ss";
    private static final String datePattern = "yyyy-MM-dd";
    private static final String dataTimePattern = datePattern + " " + timePattern;
    public static final String data_sdf_str = "yyyy年MM月dd日";
    public static final String DATE_TIME_FORMAT_I = "yyyyMMddHHmmss";
    public static final String DATE_FORMAT_I = "HH:mm";
    public static final SimpleDateFormat data_sdf = new SimpleDateFormat("yyyy年MM月dd日");

    private static ThreadLocal<SimpleDateFormat> formatDate = new ThreadLocal<SimpleDateFormat>();

    private static ThreadLocal<SimpleDateFormat> formatTime = new ThreadLocal<SimpleDateFormat>();

    private static ThreadLocal<SimpleDateFormat> formatTmpl = new ThreadLocal<SimpleDateFormat>();

    public static String getCurrentDateTimeString() {
        return DateUtils.format(new Date(), DateUtils.DATE_TIME_FORMAT);
    }
    public static String getCurrentDateTimeString2() {
        return DateUtils.format(new Date(), DateUtils.DATE_TIME_FORMAT2);
    }

    public static String changeDateFormat(Date lastModified){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd:HH-mm-ss");
        return sdf.format(lastModified);
    }

    public static SimpleDateFormat getDateFormat() {
        SimpleDateFormat fmt = formatDate.get();
        if (fmt == null) {
            fmt = new SimpleDateFormat(DATE_FORMAT);
            formatDate.set(fmt);
        }
        return fmt;
    }

    public static SimpleDateFormat getTimeFormat() {
        SimpleDateFormat fmt = formatTime.get();
        if (fmt == null) {
            fmt = new SimpleDateFormat(DATE_TIME_FORMAT);
            formatTime.set(fmt);
        }
        return fmt;
    }

    private static SimpleDateFormat getTmplFormat() {
        SimpleDateFormat fmt = formatTmpl.get();
        if (fmt == null) {
            fmt = new SimpleDateFormat(DATE_FORMAT);
            formatTmpl.set(fmt);
        }
        return fmt;
    }

    public static String format(Date date) {
        if (date == null) {
            return "";
        }
        return getDateFormat().format(date);
    }

    public static String format(Date date, String pattern) {
        SimpleDateFormat fmt = getTmplFormat();
        fmt.applyPattern(pattern);
        return fmt.format(date);
    }

    public static Date parse(String source) throws ParseException {
        return getDateFormat().parse(source);
    }

    public static Date noErrorParse(String source) {
        try {
            return getDateFormat().parse(source);
        } catch (Exception e) {
        }
        return null;
    }

    public static Date parse(String source, String pattern)
            throws ParseException {
        SimpleDateFormat fmt = getTmplFormat();
        fmt.applyPattern(pattern);
        return fmt.parse(source);
    }

    public static Date noErrorParse(String source, String pattern) {
        try {
            SimpleDateFormat fmt = getTmplFormat();
            fmt.applyPattern(pattern);
            return fmt.parse(source);
        } catch (Exception e) {
        }
        return null;
    }


    public static String toDateString(Date date) {
        return getDateFormat().format(date);
    }

    public static String toDateTimeString(Date date) {
        return getTimeFormat().format(date);
    }

    public static int isEqual(Date DATE1, Date DATE2) {
        try {
            SimpleDateFormat fmt = getDateFormat();
            String str1 = fmt.format(DATE1);
            String str2 = fmt.format(DATE2);

            Date dt1 = fmt.parse(str1);
            Date dt2 = fmt.parse(str2);

            double diff = (dt1.getTime() - dt2.getTime()) * 1d;
            return (int) Math.signum(diff);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    public static String getDiff(Date startDate, Date endDate) {
        long diff = (endDate.getTime() - startDate.getTime());// 得到两者的毫秒数
        diff = Math.abs(diff);
        long day = diff / (24 * 60 * 60 * 1000);
        long hour = (diff / (60 * 60 * 1000) - day * 24);
        long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long ms = (diff - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
                - min * 60 * 1000 - s * 1000);

        StringBuilder sb = new StringBuilder();
        if (day > 0) {
            sb.append(getDiffEn(day, "天"));
        }

        if (hour > 0 || sb.length() > 0) {
            sb.append(getDiffEn(hour, "小时"));
        }

        if (min > 0 || sb.length() > 0) {
            sb.append(getDiffEn(min, "分"));
        }

        if (s > 0 || sb.length() > 0) {
            sb.append(getDiffEn(s, "秒"));
        }
        sb.append(getDiffEn(ms, "毫秒"));
        return sb.toString();
    }

    public static String getDiffEn(Date startDate, Date endDate) {
        long diff = (endDate.getTime() - startDate.getTime());// 得到两者的毫秒数
        diff = Math.abs(diff);
        long day = diff / (24 * 60 * 60 * 1000);
        long hour = (diff / (60 * 60 * 1000) - day * 24);
        long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long ms = (diff - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);

        StringBuilder sb = new StringBuilder();
        if (day > 0) {
            sb.append(getDiffEn(day, "day"));
        }

        if (hour > 0 || sb.length() > 0) {
            sb.append(getDiffEn(hour, "hour"));
        }

        if (min > 0 || sb.length() > 0) {
            sb.append(getDiffEn(min, "minute"));
        }

        if (s > 0 || sb.length() > 0) {
            sb.append(getDiffEn(s, "second"));
        }
        sb.append(getDiffEn(ms, "millisecond"));
        return sb.toString();
    }

    private static String getDiffEn(long diff, String unit) {
        if(diff <= 1){
            return String.format("%d %s ", diff, unit);
        }
        return String.format("%d %ss ", diff, unit);
    }

    public static Date toTime(String string) throws ParseException {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat(timePattern);
            return timeFormat.parse(string);
        }
        catch (ParseException e) {

        }
        return null;
    }

    public static Date toDateTime(String string) throws ParseException {
        try {
            if(!StringUtils.isEmpty(string)){
                if (string.indexOf("T") > -1) {
                    return new SimpleDateFormat(datePattern + "'T'" + timePattern).parse(string);
                }
                else if (string.indexOf("t") > -1) {
                    return new SimpleDateFormat(datePattern + "'t'" + timePattern).parse(string);
                }
                return new SimpleDateFormat(dataTimePattern).parse(string);
            }
        }
        catch (ParseException e) {

        }
        return null;
    }

    /**
     *
     * @param 要转换的毫秒数
     * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
     * @author fy.zhang
     */
    public static String formatDuring(long mss) {
        long days = mss / (1000 * 60 * 60 * 24);
        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (mss % (1000 * 60)) / 1000;
        return days + " days " + hours + " hours " + minutes + " minutes "
                + seconds + " seconds ";
    }

    public static  String getRandom(){
        String trandNo = String.valueOf((Math.random() * 9 + 1) * 1000000);//4725110.860270676
        trandNo = trandNo.toString().substring(0, 6);
        return trandNo ;
    }

    //9位纯 数字
    public static  String getOrderNo(){
        //String orderNo = "" ;
        //UUID uuid = UUID.randomUUID();
        //String trandNo = String.valueOf((Math.random() * 9 + 1) * 1000000);
        String orderNo = "" ;
        String trandNo = String.valueOf((Math.random() * 9 + 1) * 1000000);//4725110.860270676
        String sdf = new SimpleDateFormat("MMSS").format(new Date());
        orderNo = trandNo.toString().substring(0, 4);
        orderNo =  sdf +  orderNo;
        return orderNo ;
    }

    //19纯 数字
    public static  String getOrderNoDate(){
        //String orderNo = "" ;
        //UUID uuid = UUID.randomUUID();
        //String trandNo = String.valueOf((Math.random() * 9 + 1) * 1000000);
        String orderNo = "" ;
        String trandNo = String.valueOf((Math.random() * 9 + 1) * 1000000);//4725110.860270676
        String sdf = new SimpleDateFormat("yyyyMMddHHMMSS").format(new Date());
        orderNo = trandNo.toString().substring(0, 4);
        orderNo = sdf+ orderNo;
        return orderNo ;
    }
}
